package com.sun.controller;

import com.sun.pojo.Books;
import com.sun.service.impl.BookServiceImpl;
import org.apache.xerces.util.SynchronizedSymbolTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 烛龙神
 * @Description TODO
 * @date 2021-04-10 20:33
 */
@Controller
public class BookController {

    @Autowired
    private BookServiceImpl bookServiceimpl;

    @RequestMapping("/allBook")
    public String Test(Model model)
    {
        List<Books> bookList = bookServiceimpl.queryAllBook();
        model.addAttribute("bookList",bookList);
        return "allBook";
    }

    //跳转到增加书籍页面
    @RequestMapping("/addBook")
    public String toAddPager()
    {
        return "addBook";
    }

    //添加书籍的请求
    @RequestMapping("DoAddBook")
    public String addBook(Books books)
    {
        System.out.println("addBook=>"+books);
        bookServiceimpl.addBook(books);
        return "redirect:/allBook";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePager(Model model,int id)
    {
   //     System.out.println(bookID);
        Books books = bookServiceimpl.queryBookById(id);
        model.addAttribute("QBook",books);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books books)
    {
        System.out.println("updateBook=>"+books);
        bookServiceimpl.updateBook(books);
        return "redirect:/allBook";
    }

    //刪除书籍
    @RequestMapping("/toDelete")
    public String deleteBook(int id)
    {
        bookServiceimpl.deleteBookById(id);
        return "redirect:/allBook";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model) {
        Books books = bookServiceimpl.queryBookByName(queryBookName);
        List<Books> bookList = new ArrayList<>();
        bookList.add(books);
        if (books == null)
        {
            bookList = bookServiceimpl.queryAllBook();
            model.addAttribute("error", "未查到");
        }
        model.addAttribute("bookList", bookList);
        return "allBook";
    }
}
